import java.sql.*;
import java.sql.Date;
import java.util.*;

/**
 * Appointment class. Created by patients containing the desired time,
 * appointment reason, and desired doctor. Nurse or doctor can add a
 * treatmentRecord, and will then be added as attendingNurse or attendingDoctor
 */
public class Appointment {
	private String username;
	private int treatmentRecordId;
	private Calendar time;
	private String reason;
	private Doctor desiredDoctor;

	private Doctor attendingDoctor;
	private Nurse attendingNurse;
	// the username of the desired doctor and attending nurse
	private String doctor;
	private String nurse;

	/**
	 * Information holder for appointments.
	 * 
	 * @param record
	 *            The treatment record that Appointment belongs to.
	 */
	public Appointment(int recordId) {
		this.treatmentRecordId = recordId;
		this.time = new GregorianCalendar();
		this.username = "";
		this.reason = null;
		this.desiredDoctor = null;

		this.attendingDoctor = null;
		this.attendingNurse = null;
		this.doctor = null;
		this.nurse = null;
	}

	/**
	 * Information holder for appointments.
	 */
	public Appointment() {
		this.time = new GregorianCalendar();
		this.username = "";
		this.reason = null;
		this.desiredDoctor = null;

		this.attendingDoctor = null;
		this.attendingNurse = null;
		this.doctor = null;
		this.nurse = null;
	}

	/**
	 * Gets treatment record
	 * 
	 * @return treatment record it belongs to
	 */
	public TreatmentRecord getTreatmentRecord() {
		return TreatmentRecord.readTreatmentRecord(treatmentRecordId);
	}

	/**
	 * Sets treatment record
	 * 
	 * @param record
	 *            Treament record it should belong to
	 */
	public void setTreatmentRecord(int recordId) {
		this.treatmentRecordId = recordId;
	}

	/**
	 *Gets the username
	 * 
	 * @return String username
	 */
	public String getUsername() {
		return this.username;
	}

	/**
	 * Sets the username
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * Gets time of appointment
	 * 
	 * @return time of appointment
	 */
	public Calendar getTime() {
		return time;
	}

	/**
	 * Sets time of appointment
	 * 
	 * @param time
	 *            Calendar of appointment
	 */
	public void setTime(Calendar time) {
		this.time = time;
	}

	/**
	 * Gets reason for appointment
	 * 
	 * @return reason for appointment
	 */
	public String getReason() {
		return reason;
	}

	/**
	 * Sets reason for appointment
	 * 
	 * @param reason
	 *            Reason for appointment
	 */
	public void setReason(String reason) {
		this.reason = reason;
	}

	/**
	 * Gets desired doctor
	 * 
	 * @return desired doctor
	 */
	public Doctor getDesiredDoctor() {
		return desiredDoctor;
	}

	/**
	 * Sets desired doctor
	 * 
	 * @param doctor
	 *            Desired doctor
	 */
	public void setDesiredDoctor(Doctor doctor) {
		this.desiredDoctor = doctor;
	}

	public String getDoctor() {
		return doctor;
	}

	public void setDoctor(String doctor) {
		this.doctor = doctor;
	}

	/**
	 * Gets the attending nurse that the appointment belongs to.
	 * 
	 * @return attending nurse
	 */
	public Nurse getAttendingNurse() {
		return attendingNurse;
	}

	/**
	 * Sets attending nurse
	 * 
	 * @param nurse
	 *            Attending nurse
	 */
	public void setAttendingNurse(Nurse nurse) {
		this.attendingNurse = nurse;
	}

	public String getNurse() {
		return nurse;
	}

	public void setNurse(String nurse) {
		this.nurse = nurse;
	}

	// Database CRUD functionality goes below here

	// Create
	public static boolean createAppointment(String username, java.sql.Date date, java.sql.Time time/*, String doctor, String nurse, String reason, int recordId*/){
//    String sql = "INSERT INTO Appointments VALUES ('" + username + "', '" + date + "', '" + time + "', '" + reason + "', '" + doctor + "', '" + nurse + "', '" + recordId*/ + "')";
    String sql = "INSERT INTO Appointments VALUES ('" + username + "', '" + date + "', '" + time + "', 'A reason', 'Dr. Somebody', 'Nurse Nurse', 3)";
    if(Database.query(sql)==1)
      return true;
    else{	
      return false;
    }
  }

	// Create
	public static boolean createAppointment(String username, java.sql.Date date, java.sql.Time time, String doctor, String nurse, String reason, int recordId){
    String sql = "INSERT INTO Appointments VALUES ('" + username + "', '" + date + "', '" + time + "', '" + reason + "', '" + doctor + "', '" + nurse + "', '" + recordId*/ + "')";
    if(Database.query(sql)==1)
      return true;
    else{	
      return false;
    }
  }

	public static int findAppointmentId(String username, java.sql.Date date,
			java.sql.Time time) {
		String sql = "SELECT appointment_id FROM Appointments WHERE username='"
				+ username + "' AND appt_date='" + date + "' AND appt_time='"
				+ time + "'";
		ResultSet rs = Database.selectQuery(sql);
		int foundId = -1;
		// should be 1 appointment per id
		try {
			while (rs.next()) {
				foundId = rs.getInt("appointment_id");
			}
		} catch (Exception e) {
		}
		return foundId;
	}

	// Read
	public static Collection<Appointment> readAppointment(String username) {
		String sql = "SELECT * FROM Appointments WHERE username='" + username
				+ "'";
		ResultSet rs = Database.selectQuery(sql);
		Collection<Appointment> returnvar = new ArrayList<Appointment>();
		while (rs.next()) {
			TreatmentRecord currentTreatmentRecord = Patient.readPatient(
					username).getPatientInfo().getMedicalHistory();
			// Fix to take treatment record id
			Appointment current = new Appointment(currentTreatmentRecord);
			java.sql.Date foundDate = rs.getDate("appt_date");
			java.sql.Time foundTime = rs.getTime("appt_time");
			current.setTime(new GregorianCalendar(foundDate.getYear(),
					foundDate.getMonth(), foundDate.getDay(), foundTime
							.getHours(), foundTime.getMinutes(), foundTime
							.getSeconds()));
			current.setDoctor(rs.getString("doctor"));
			current.setNurse(rs.getString("nurse"));
			current.setReason(rs.getString("reason"));
			// WORKMARK
			returnvar.add(current);
		}
		return returnvar;
	}

	// Read using appointment_id
	@SuppressWarnings("deprecation")
	public static Appointment readAppointment(int id) {
		java.sql.Date foundDate = null;
		Time foundTime = null;
		String foundReason = "";
		String foundDoctor = "";
		String foundNurse = "";
		int recordId = 0;
		String sql = "SELECT * FROM Appointments WHERE appointment_id=" + id;
		ResultSet rs = Database.selectQuery(sql);
		// should be 1 appointment per id
		try {
			while (rs.next()) {
				foundDate = rs.getDate("appt_date");
				foundTime = rs.getTime("appt_time");
				foundReason = rs.getString("reason");
				foundDoctor = rs.getString("doctor");
				foundNurse = rs.getString("nurse");
				recordId = rs.getInt("treatment_record");
			}
		} catch (Exception e) {
		}
		Appointment appointment = new Appointment(recordId);

		appointment.setTime(new GregorianCalendar(foundDate.getYear(),
				foundDate.getMonth(), foundDate.getDay(), foundTime.getHours(),
				foundTime.getMinutes(), foundTime.getSeconds()));
		appointment.setReason(foundReason);
		appointment.setDoctor(foundDoctor);
		appointment.setNurse(foundNurse);
		return appointment;
	}

	// Update
	// Figure out how to update only 1 appointment instead of all appointments
	// for that user
	public static boolean updateAppointment(String username,
			java.sql.Date date, java.sql.Time time, String doctor,
			String nurse, String reason, int recordId) {
		String sql = "UPDATE Appointments SET doctor='" + doctor + "', nurse='"
				+ nurse + "', reason='" + reason + "', treatment_record='"
				+ recordId + "' WHERE username='" + username + "', appt_date='"
				+ date + "', appt_time='" + time + "'";
		if (Database.query(sql) > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// Delete
	public static boolean deleteAppointment(String username,
			java.sql.Date date, java.sql.Time time) {
		String sql = "DELETE FROM Appointments WHERE username='" + username
				+ "' AND appt_date='" + date + "' AND appt_time='" + time + "'";
		if (Database.query(sql) == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// Delete
	public static boolean deleteAppointment(int appointmentId) {
		String sql = "DELETE FROM Appointments WHERE appointment_id='"
				+ appointmentId + "'";
		if (Database.query(sql) == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
